#!/usr/bin/node

const fs  = require('fs'),
      pic = process.argv[2],
      path = require('path'),
      http = require('http');

if(process.argv.length!==3){
  console.err('命令行参数格式：cmd fileName');
  process.exit(1);
}

try {
  var data = fs.readFileSync(pic).toString('base64');
} catch(e) {
  console.error(e.message);
  process.exit(2);
}
//用uriData
let ext = path.extname(pic);
let uriData = 'data:image/' + ext.slice(1,ext.length) + ';base64,' + data;
// console.log('data uri:\n%s', uriData);

let html =
` <html>
  <head>
    <title>data uri</title>
  </head>
  <body>
    <img alt=${path.basename(pic,ext)} src=${uriData}>
  </body>
  </html>
`;

http.createServer((req,res) => {
  res.end(html);
}).listen(8080);
